package cn.easytogo.util;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日期操作工具类，提供常见的日期操作需要的方法。
 */

public class DateUtil {
	/**
	 * 此类中封装一些常用的日期操作。 所有方法都是静态方法，不需要生成此类的实例， 为避免生成此类的实例，构造方法被申明为private类型的。
	 */
	private DateUtil() {
	}

	/**
	 * 获取当前系统时间，格式'yyyy-mm-dd'
	 * 
	 * @return 当前系统时间
	 */
	public static String getLocalDate() {
		return format(new Date());
	}

	public static Date getDateTime() {
		return new Date(System.currentTimeMillis());
	}

	/**
	 * 获取指定格式的系统时间字符串
	 * 
	 * @param dateFormat
	 *            日期格式
	 * @return 指定格式的系统时间
	 */
	public static String getLocalDate(String dateFormat) {
		return format(getDate(), dateFormat);
	}

	/**
	 * 返回当前系统时间
	 * 
	 * @return 系统时间
	 */
	public static Date getDate() {
		return new Date();
	}

	/**
	 * 返回当前系统日期、时间
	 * 
	 * @param format
	 *            返回日期的格式
	 * @return 系统日期、时间
	 */
	public static Date getDate(String format) {
		return parse(getLocalDate(format));
	}

	/**
	 * 格式化日期 'yyyy-mm-dd'
	 * 
	 * @param date
	 *            待格式化的日期
	 * @return 日期字符串格式 'yyyy-mm-dd',date为空时返回""
	 */
	public static String format(Date date) {
		return format(date, "yyyy-MM-dd");
	}

	/**
	 * 格式：dateFormat 指定的格式
	 * 
	 * @param date
	 *            待格式化的日期
	 * @param dateFormat
	 *            日期格式
	 * @return 日期的字符串格式，date为空时返回""
	 */
	public static String format(Date date, String dateFormat) {
		if (date == null)
			return "";
		Format formatter = new SimpleDateFormat(dateFormat);
		return formatter.format(date);
	}

	/**
	 * 格式：dateFormat 指定的格式
	 * 
	 * @param date
	 *            待格式化的日期
	 * @param dateFormat
	 *            日期格式
	 * @param defaultRtn
	 *            date为空时的默认返回值
	 * @return 日期的字符串格式
	 */
	public static String format(Date date, String dateFormat, String defaultRtn) {
		if (defaultRtn == null)
			defaultRtn = "";
		if (date == null)
			return defaultRtn;
		Format formatter = new SimpleDateFormat(dateFormat);
		return formatter.format(date);
	}

	/**
	 * 将"yyyy-MM-dd"格式的字符串格式化为Date类型
	 * 
	 * @param str
	 *            待处理的字符串
	 * @return 日期，若出现异常ParseException则返回 null
	 */
	public static Date parse(String str) {
		return parse(str, "yyyy-MM-dd");
	}

	/**
	 * 将指定格式的字符串格式化为Date类型
	 * 
	 * @param str
	 *            待处理的字符串
	 * @param dateFormat
	 *            日期的格式
	 * @return 日期，若出现异常ParseException则返回 null
	 */
	public static Date parse(String str, String dateFormat) {
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		try {
			if (str == null) {
				return null;
			}
			return format.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 根据日历的规则，为给定的日历字段添加或减去指定的时间量。
	 * 
	 * @param date
	 *            日期
	 * @param field
	 *            日历字段。
	 * @param amount
	 *            为字段添加的日期或时间量。
	 * @return 修改后的日期
	 */
	public static Date add(Date date, int field, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, amount);
		return calendar.getTime();
	}

	/**
	 * 此方法将两个日期之间的时间以dateFormat的格式转换为字符串数组
	 * 
	 * @param dateStart
	 *            起始日期
	 * @param dateEnd
	 *            终止日期
	 * @param dateFormat
	 *            日期格式
	 * @return 日期的字符串数组
	 */
	public static String[] splitDay(String dateStart, String dateEnd,
			String dateFormat) {
		String[] wordLists;

		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		Calendar grcStart = new GregorianCalendar();
		Calendar grcEnd = new GregorianCalendar();

		if (dateStart == null || dateEnd == null) {
			wordLists = new String[1];
			wordLists[0] = dateStart;
			return wordLists;
		}
		try {
			grcStart.setTime(format.parse(dateStart));
		} catch (ParseException e) {
			grcStart = new GregorianCalendar();
		}
		try {
			grcEnd.setTime(format.parse(dateEnd));
		} catch (ParseException ex) {
			grcEnd = new GregorianCalendar();
		}
		return splitDay(grcStart, grcEnd, dateFormat);
	}

	/**
	 * 此方法将两个日期之间的时间以dateFormat的格式转换为字符串数组
	 * 
	 * @param grcStart
	 *            起始日期
	 * @param grcEnd
	 *            终止日期
	 * @param dateFormat
	 *            日期格式
	 * @return 日期的字符串数组
	 */
	public static String[] splitDay(Calendar grcStart, Calendar grcEnd,
			String dateFormat) {
		String[] wordLists;
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		GregorianCalendar grcTmp = new GregorianCalendar();
		grcTmp.setTime(grcStart.getTime());
		int total = 0;
		while (grcTmp.get(Calendar.YEAR) < grcEnd.get(Calendar.YEAR)) {
			if (grcTmp.isLeapYear(grcTmp.get(Calendar.YEAR)))
				total += 365;
			else
				total += 366;
			grcTmp.add(Calendar.YEAR, 1);
		}
		if (grcTmp.get(Calendar.YEAR) == grcEnd.get(Calendar.YEAR))
			total += grcEnd.get(Calendar.DAY_OF_YEAR)
					- grcTmp.get(Calendar.DAY_OF_YEAR);
		if (total <= 0)
			return null;
		wordLists = new String[total];
		for (int i = 0; i < total; i++) {
			wordLists[i] = format.format(grcStart.getTime());
			grcStart.add(Calendar.DATE, 1);
		}
		return wordLists;
	}

	/**
	 * 此方法将两个日期之间的时间以monthFormat的格式转换为字符串数组
	 * 
	 * @param startMonth
	 *            起始日期
	 * @param endMonth
	 *            终止日期
	 * @param monthFormat
	 *            日期格式
	 * @return 日期的字符串数组
	 */
	public static String[] splitMonth(String startMonth, String endMonth,
			String monthFormat) {
		String[] wordLists;

		SimpleDateFormat format = new SimpleDateFormat(monthFormat);
		Calendar grcStart = new GregorianCalendar();
		Calendar grcEnd = new GregorianCalendar();

		if (startMonth == null || endMonth == null) {
			wordLists = new String[1];
			wordLists[0] = startMonth;
			return wordLists;
		}
		try {
			grcStart.setTime(format.parse(startMonth));
		} catch (ParseException e) {
			grcStart = new GregorianCalendar();
		}
		try {
			grcEnd.setTime(format.parse(endMonth));
		} catch (ParseException ex) {
			grcEnd = new GregorianCalendar();
		}
		return splitMonth(grcStart, grcEnd, monthFormat);
	}

	/**
	 * 此方法将两个日期之间的时间以dateFormat的格式转换为字符串数组
	 * 
	 * @param grcStart
	 *            起始日期
	 * @param grcEnd
	 *            终止日期
	 * @param dateFormat
	 *            日期格式
	 * @return 日期的字符串数组
	 */
	public static String[] splitMonth(Calendar grcStart, Calendar grcEnd,
			String dateFormat) {
		String[] wordLists;
		SimpleDateFormat format = new SimpleDateFormat(dateFormat);
		GregorianCalendar grcTmp = new GregorianCalendar();
		grcTmp.setTime(grcStart.getTime());
		int total = 0;
		while (grcTmp.get(Calendar.YEAR) < grcEnd.get(Calendar.YEAR)) {
			total += 12;
			grcTmp.add(Calendar.YEAR, 1);
		}
		if (grcTmp.get(Calendar.YEAR) == grcEnd.get(Calendar.YEAR))
			total += grcEnd.get(Calendar.MONTH) - grcTmp.get(Calendar.MONTH);
		if (total <= 0)
			return null;
		total++;
		wordLists = new String[total];
		for (int i = 0; i < total; i++) {
			wordLists[i] = format.format(grcStart.getTime());
			grcStart.add(Calendar.MONTH, 1);
		}
		return wordLists;
	}

	/**
	 * 计算两个日期之间的日期差值
	 * 
	 * @param date1
	 *            日期1
	 * @param date2
	 *            日期2
	 * @return 日期1-日期2 的值
	 * @since 1.1
	 */
	public static int differenceOfDay(Date date1, Date date2) {
		int sign = date1.compareTo(date2);
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		if (sign > 0) {
			cal1.setTime(date1);
			cal2.setTime(date2);
		} else {
			cal1.setTime(date2);
			cal2.setTime(date1);
		}
		int total = 0;
		while (cal2.get(Calendar.YEAR) < cal1.get(Calendar.YEAR)) {
			total += cal2.getActualMaximum(Calendar.DAY_OF_YEAR);
			cal2.add(Calendar.YEAR, 1);
		}
		if (cal2.get(Calendar.YEAR) == cal1.get(Calendar.YEAR)) {
			total += cal1.get(Calendar.DAY_OF_YEAR)
					- cal2.get(Calendar.DAY_OF_YEAR);
		}
		return sign * total;
	}

	/**
	 * 根据日历的规则，获取给定的日期所在旬差值为amount的旬度的第一日。
	 * 
	 * @param date
	 *            日期
	 * @param amount
	 *            与日期所在旬的差值量
	 * @return 根据日历的规则，获取给定的日期所在旬差值为amount的旬度的第一日
	 * @since 1.1
	 */
	public static Date getFirstDayPeriodOfTenDays(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int xunCount = (calendar.get(Calendar.DAY_OF_MONTH) - 1) / 10;
		if (xunCount == 3)
			xunCount = 2;
		int monthDiff;
		if ((xunCount + amount) < 0) {
			if ((xunCount + amount) / 3 == 0)
				monthDiff = ((xunCount + amount) / 3 - 1);
			else
				monthDiff = (xunCount + amount) / 3;
		} else {
			monthDiff = (xunCount + amount) / 3;
		}
		calendar.setTime(DateUtil.add(calendar.getTime(), Calendar.MONTH,
				monthDiff));
		if ((xunCount + amount) % 3 == 0) {
			xunCount = 0;
		} else if ((xunCount + amount) % 3 < 0) {
			xunCount = (xunCount + amount) % 3 + 3;
		} else {
			xunCount = (xunCount + amount) % 3;
		}
		switch (xunCount) {
		case 0:
			calendar.set(Calendar.DAY_OF_MONTH, 1);
			break;
		case 1:
			calendar.set(Calendar.DAY_OF_MONTH, 11);
			break;
		default:
			calendar.set(Calendar.DAY_OF_MONTH, 21);
		}
		return calendar.getTime();
	}

	/**
	 * 根据日历的规则，获取给定的日期所在旬差值为amount的旬度的最后一日。
	 * 
	 * @param date
	 *            日期
	 * @param amount
	 *            与日期所在旬的差值量
	 * @return 根据日历的规则，获取给定的日期所在旬差值为amount的旬度的最后一日
	 * @since 1.1
	 */
	public static Date getEndDayPeriodOfTenDays(Date date, int amount) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int xunCount = (calendar.get(Calendar.DAY_OF_MONTH) - 1) / 10;
		if (xunCount == 3)
			xunCount = 2;
		int monthDiff;
		if ((xunCount + amount) < 0) {
			if ((xunCount + amount) / 3 == 0)
				monthDiff = ((xunCount + amount) / 3 - 1);
			else
				monthDiff = (xunCount + amount) / 3;
		} else {
			monthDiff = (xunCount + amount) / 3;
		}
		calendar.setTime(DateUtil.add(calendar.getTime(), Calendar.MONTH,
				monthDiff));
		if ((xunCount + amount) % 3 == 0) {
			xunCount = 0;
		} else if ((xunCount + amount) % 3 < 0) {
			xunCount = (xunCount + amount) % 3 + 3;
		} else {
			xunCount = (xunCount + amount) % 3;
		}
		switch (xunCount) {
		case 0:
			calendar.set(Calendar.DAY_OF_MONTH, 10);
			break;
		case 1:
			calendar.set(Calendar.DAY_OF_MONTH, 20);
			break;
		default:
			calendar.set(Calendar.DAY_OF_MONTH, calendar
					.getActualMaximum(Calendar.DAY_OF_MONTH));
		}
		return calendar.getTime();
	}

	/**
	 * 计算两个日期之间的差,单位是天 Chivas Ju. zhuy#slof.com
	 * 
	 * @param date1
	 * @param date2
	 * @return long todo 待验证
	 */
	private static long getDays(Date date1, Date date2) {
		long quot = 0;
		try {
			quot = date1.getTime() - date2.getTime();
			quot = quot / 1000 / 60 / 60 / 24;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Math.abs(quot);

	}

	// 第N周开始日期
	// todo 待验证
	private static Date getStartTime(int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		int k = calendar.get(Calendar.DAY_OF_WEEK);
		calendar.add(Calendar.DAY_OF_YEAR, 1 - k);
		calendar.add(Calendar.WEEK_OF_YEAR, i - 1);
		return calendar.getTime();
	}

	// 第N周结束日期
	// todo 待验证
	private static Date getEndTime(int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_YEAR, 1);
		int k = calendar.get(Calendar.DAY_OF_WEEK);
		calendar.add(Calendar.DAY_OF_YEAR, 1 - k);
		calendar.add(Calendar.WEEK_OF_YEAR, i - 1);
		calendar.add(Calendar.DAY_OF_YEAR, 6);
		return calendar.getTime();
	}

	/**
	 * 返回年
	 * 
	 * @param q
	 * @return
	 */
	public static String getYear(Date q) {
		return format(q, "yyyy");
	}

	/**
	 * 返回月
	 * 
	 * @param q
	 * @return
	 */
	public static String getMonth(Date q) {
		return format(q, "MM");
	}

	/**
	 * 返回日子
	 * 
	 * @param q
	 * @return
	 */
	public static String getDay(Date q) {
		return format(q, "dd");
	}

	/**
	 * 增加日期
	 * 
	 * @param date
	 *            日期
	 * @param field
	 *            the calendar field
	 * @param i
	 *            天数
	 * @return 添加i天后的日期
	 */
	public static Date addDate(Date date, int field, int i) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(field, i);
		return calendar.getTime();
	}

	/**
	 * 增加日期
	 * 
	 * @param date
	 *            日期
	 * @param i
	 *            天数
	 * @return 添加i天后的日期
	 */
	public static Date addDate(Date date, int i) {
		return addDate(date, Calendar.DATE, i);
	}

	/**
	 * 增加日期
	 * 
	 * @param date
	 *            日期
	 * @param i
	 *            月数
	 * @return 添加i月后的日期
	 */
	public static Date addMonth(Date date, int i) {
		return addDate(date, Calendar.MONTH, i);
	}

	/**
	 * 增加日期
	 * 
	 * @param date
	 *            日期
	 * @param i
	 *            年数
	 * @return 添加i年后的日期
	 */
	public static Date addYear(Date date, int i) {
		return addDate(date, Calendar.YEAR, i);
	}

	/**
	 * 传入年月，获取旬度的开始结束时间
	 * 
	 * @param year
	 * @param month
	 * @param param
	 *            1-上旬；2-中旬；3-下旬
	 * @return 数组第一个为开始时间，第二个为结束时间
	 */
	public static Date[] getTenDay(String year, String month, int param) {
		Date[] date = new Date[2];

		if (param == 1) {
			date[0] = parse(year + "-" + month + "-01");
			date[1] = parse(year + "-" + month + "-10");
		} else if (param == 2) {
			date[0] = parse(year + "-" + month + "-11");
			date[1] = parse(year + "-" + month + "-20");
		} else if (param == 3) {
			date[0] = parse(year + "-" + month + "-21");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date[0]);
			date[1] = parse(year + "-" + month + "-"
					+ (calendar.getActualMaximum(Calendar.DAY_OF_MONTH)));
		}

		return date;
	}

	/**
	 * 传入年月，获取旬度的开始结束时间
	 * 
	 * @param day
	 *            日期格式必须为'yyyymmdd'形式
	 * @return 数组第一个为开始时间，第二个为结束时间
	 * @author yuanl
	 */
	public static String[] getTenDay2(String day) {
		String year = day.substring(0, 4);
		String month = day.substring(4, 6);
		// int xundu = Integer.parseInt(day.substring(6));
		int param = Integer.parseInt(day.substring(7, 8));
		// if("01".equals(xundu)){
		// param=1;
		// }else if("02".equals(xundu)){
		// param=2;
		// }else if("03".equals(xundu)){
		// param=3;
		// }
		String[] strings = new String[4];
		if (param == 1) {
			strings[0] = year + "-" + month + "-01";
			strings[1] = year + "-" + month + "-10";
			strings[2] = "10";
		} else if (param == 2) {
			strings[0] = year + "-" + month + "-11";
			strings[1] = year + "-" + month + "-20";
			strings[2] = "10";
		} else if (param == 3) {
			strings[0] = year + "-" + month + "-21";
			Date date = parse(year + "-" + month + "-21");
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			strings[1] = year + "-" + month + "-"
					+ (calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
			int xz = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) - 20;
			strings[2] = Integer.toString(xz);
		}
		strings[3] = Integer.toString(param);
		return strings;
	}

	public static int getLastDayOfMonth(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public static String getXd(Date date) {
		String rtn = "";
		if (Integer.parseInt(DateUtil.getDay(date)) >= 1
				&& Integer.parseInt(DateUtil.getDay(date)) < 11) {
			rtn = "1";
		} else if (Integer.parseInt(DateUtil.getDay(date)) > 10
				&& Integer.parseInt(DateUtil.getDay(date)) < 21) {
			rtn = "2";
		} else {
			rtn = "3";
		}
		return rtn;
	}

	/**
     * 
     */
	public static Map getLastDate(String beginTime, String endTime) {
		Calendar calStart = Calendar.getInstance();
		calStart.setTime(java.sql.Date.valueOf(beginTime));
		Calendar calEnd = Calendar.getInstance();
		calEnd.setTime(java.sql.Date.valueOf(endTime));
		Calendar calTmp = (Calendar) calStart.clone();
		Calendar calLast = (Calendar) calStart.clone();
		while (!calEnd.equals(calTmp)) {
			calTmp.add(Calendar.DATE, +1);
			calLast.add(Calendar.DATE, -1);
		}
		calLast.add(Calendar.DATE, -1);
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		Map m = new HashMap();
		m.put("sBegin", f.format(calLast.getTime()));
		calStart.add(Calendar.DATE, -1);
		m.put("sEnd", f.format(calStart.getTime()));

		return m;

	}

	public static Map<String, String> getXun(Calendar cal) {// ///////输入日期得到本旬和上旬
		Map<String, String> m = new HashMap<String, String>();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat f1 = new SimpleDateFormat("MM");
		if (cal.get(Calendar.DATE) < 10) {// //当前输入日期小于10天 本期取上月下旬
			Calendar c = Calendar.getInstance();
			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, 21);
			m.put("thisBegin", f.format(c.getTime()));
			c.set(Calendar.YEAR, cal.get(Calendar.YEAR));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
			c.set(Calendar.DATE, 1);
			c.add(Calendar.DATE, -1);// 取上月最后一天
			m.put("thisEnd", f.format(c.getTime()));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
			m.put("thisTitle", f1.format(c.getTime()) + "下旬");

			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, 11);
			m.put("lastBegin", f.format(c.getTime()));
			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, 20);
			m.put("lastEnd", f.format(c.getTime()));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
			m.put("lastTitle", f1.format(c.getTime()) + "中旬");
		} else if (cal.get(Calendar.DATE) >= 10 && cal.get(Calendar.DATE) < 20) {// //当前输入日期小于20天
																					// 本期取本月上旬
			Calendar c = Calendar.getInstance();
			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
			m.put("thisBegin", f.format(c.getTime()));
			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 10);
			m.put("thisEnd", f.format(c.getTime()));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
			m.put("thisTitle", f1.format(c.getTime()) + "上旬");

			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, 21);
			m.put("lastBegin", f.format(c.getTime()));
			c.set(Calendar.YEAR, cal.get(Calendar.YEAR));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
			c.set(Calendar.DATE, 1);
			c.add(Calendar.DATE, -1);// 取上月最后一天
			m.put("lastEnd", f.format(c.getTime()));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
			m.put("lastTitle", f1.format(c.getTime()) + "下旬");
		} else if (cal.get(Calendar.DATE) >= 20) {// //当前输入日期大于20天 本期取本月中旬
			Calendar c = Calendar.getInstance();
			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 11);
			m.put("thisBegin", f.format(c.getTime()));
			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 20);
			m.put("thisEnd", f.format(c.getTime()));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
			m.put("thisTitle", f1.format(c.getTime()) + "中旬");

			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
			m.put("lastBegin", f.format(c.getTime()));
			c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 10);
			m.put("lastEnd", f.format(c.getTime()));
			c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
			m.put("lastTitle", f1.format(c.getTime()) + "上旬");
		}
		return m;
	}

	public static Map<String, String> getMonth(Calendar cal) {// ///////输入日期得到本月和上月
		Map<String, String> m = new HashMap<String, String>();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat f1 = new SimpleDateFormat("MM");
		Calendar c = Calendar.getInstance();
		c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
		m.put("thisBegin", f.format(c.getTime()));
		c.set(Calendar.YEAR, cal.get(Calendar.YEAR));
		c.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1);
		c.set(Calendar.DATE, 1);
		c.add(Calendar.DATE, -1);// 取上月最后一天
		m.put("thisEnd", f.format(c.getTime()));
		c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
		m.put("thisTitle", f1.format(c.getTime()) + "月");

		c.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, 1);
		m.put("lastBegin", f.format(c.getTime()));
		c.set(Calendar.YEAR, cal.get(Calendar.YEAR));
		c.set(Calendar.MONTH, cal.get(Calendar.MONTH));
		c.set(Calendar.DATE, 1);
		c.add(Calendar.DATE, -1);// 取上月最后一天
		m.put("lastEnd", f.format(c.getTime()));
		c.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
		m.put("lastTitle", f1.format(c.getTime()) + "月");
		return m;
	}

	public static Map getXun(Calendar cal, int i) {
		Map m = new HashMap();
		Calendar cb = Calendar.getInstance();
		Calendar ce = Calendar.getInstance();
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		if (i == 1) {
			cb.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1);
			m.put("begin", f.format(cb.getTime()));
			ce.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 10);
			m.put("end", f.format(ce.getTime()));
		}
		if (i == 2) {
			cb.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 11);
			m.put("begin", f.format(cb.getTime()));
			ce.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 20);
			m.put("end", f.format(ce.getTime()));
		}
		if (i == 3) {
			cb.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 21);
			m.put("begin", f.format(cb.getTime()));
			ce.set(Calendar.MONTH, cal.get(Calendar.MONTH - 1));
			ce.set(Calendar.DATE, 1);
			ce.add(Calendar.DATE, -1);
			m.put("end", f.format(ce.getTime()));
		}
		return m;
	}

	public static boolean isLastDate(String date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(java.sql.Date.valueOf(date));
		boolean flag = false;
		int m = cal.get(Calendar.MONTH);
		Calendar c = (Calendar) cal.clone();
		c.add(Calendar.DATE, 1);
		int n = c.get(Calendar.MONTH);
		if (m != n) {
			flag = true;
		}
		return flag;
	}

	public static String getNextDay(String date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(java.sql.Date.valueOf(date));
		cal.add(Calendar.DATE, amount);
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		Calendar today = Calendar.getInstance();
		today.setTime(new java.util.Date());
		if (cal.getTime().getTime() > today.getTime().getTime()) {
			return f.format(today.getTime());
		} else {
			return f.format(cal.getTime());
		}
	}

	// 输入今年日期的字符串，取得去年日期的字符串
	public static String getPreYear(String date, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(java.sql.Date.valueOf(date));
		cal.add(Calendar.YEAR, amount);
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

		Calendar today = Calendar.getInstance();
		today.setTime(new java.util.Date());
		if (cal.getTime().getTime() > today.getTime().getTime()) {
			return f.format(today.getTime());
		} else {
			return f.format(cal.getTime());
		}
	}

	// 输入yyyy-mm得到数组，其中包括该月月初yyyymm01和该月月末yyyymmxx
	public static Date[] getMonthByNy(String ny) {
		Date[] date = new Date[2];
		String firstDay = ny + "01";// 当月第一天
		date[0] = DateUtil.parse(firstDay, "yyyyMMdd");
		int i = DateUtil.getLastDayOfMonth(date[0]);
		String lastday = ny + i;// 当月最后第一天
		date[1] = DateUtil.parse(lastday, "yyyyMMdd");
		return date;
	}

	// public static Date getCxsj(){
	// Date today=new Date();
	// String scxsj = cn.com.victorysoft.oadd.SysConfig.getProperty("cxsj");
	// if(scxsj==null)
	// scxsj="10:30";
	// Date time_start =
	// parse(format(today,"yyyy-MM-dd ")+scxsj,"yyyy-MM-dd HH:mm");
	// if(today.compareTo(time_start)>0)
	// return today;
	// return addDate(today, -1);
	// }
	/**
	 * 获取上周周日的日期
	 */
	public static Date getPreSun() {
		Calendar cd = Calendar.getInstance();
		int dayofWeek = cd.get(Calendar.DAY_OF_WEEK) - 1;
		Date date = new Date();
		Date preSunDate = DateUtil.addDate(date, -5);
		return preSunDate;
	}

	/**
	 * 获得周的日期（从当前日期nowrq上个周日开始）
	 * 
	 * @param nowrq
	 *            当前输入的日期格式是yyyy-mm-dd。null或空值为当前时间
	 * @param preDayNum
	 *            从上个周日开始往前数天数。即上个周日为0，上个周1为6，上上个周日为7，上上个周1为13。。。
	 * @return 字符格式的日期
	 */
	public static String getPreWeekDay(String nowrq, int preDayNum) {
		String retrunDatStr = "";
		Calendar aCalendar = Calendar.getInstance();
		if (nowrq != null && !nowrq.equals("")) {
			aCalendar.setTime(DateUtil.parse(nowrq));
		} else {
			aCalendar.setTime(new Date());
		}
		// 取当前日期是星期几(week:星期几)
		int week = aCalendar.get(Calendar.DAY_OF_WEEK);
		if (week == 1) {
			week = 7;
		} else if (week == 0) {
			week = 6;
		} else {
			week -= 1;
		}
		// 取距离当前日期之前的周日
		int count = 0;
		count = -week;

		// 获取距离当前日期最近的周日日期
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		if (nowrq != null && !nowrq.equals("")) {
			c.setTime(DateUtil.parse(nowrq));
		} else {
			c.setTime(new Date());
		}
		c.add(Calendar.DAY_OF_WEEK, count - preDayNum);
		retrunDatStr = df.format(c.getTime());
		return retrunDatStr;
	}

	/**
	 * 取输入日期的上一月数据
	 * 
	 * @param dateStr
	 *            输入日期字符串。null或空位默认当前日期
	 * @param format
	 *            字符串输入日期格式和返回格式
	 * @return
	 */
	public static String preDateMonthFormat(String dateStr, String format) {
		SimpleDateFormat dtFormat = new SimpleDateFormat(format);
		Calendar c1 = Calendar.getInstance();
		if (dateStr == null || dateStr.equals("")) {
		} else {
			c1.setTime(DateUtil.parse(dateStr, format));
		}
		c1.add(GregorianCalendar.MONTH, -1);
		String dstr = dtFormat.format(c1.getTime());
		return dstr;
	}

	/**
	 * 取输入日期的上一年数据
	 * 
	 * @param dateStr
	 *            输入日期字符串。null或空位默认当前日期
	 * @param format
	 *            字符串输入日期格式和返回格式
	 * @return
	 */
	public static String preDateYearFormat(String dateStr, String format) {
		SimpleDateFormat dtFormat = new SimpleDateFormat(format);
		Calendar c1 = Calendar.getInstance();
		if (dateStr == null || dateStr.equals("")) {
		} else {
			c1.setTime(DateUtil.parse(dateStr, format));
		}
		c1.add(GregorianCalendar.YEAR, -1);
		String dstr = dtFormat.format(c1.getTime());
		return dstr;
	}

	/**
	 * 取输入日期的上一天数据
	 * 
	 * @param dateStr
	 *            输入日期字符串。null或空位默认当前日期
	 * @param format
	 *            字符串输入日期格式和返回格式
	 * @return
	 */
	public static String preDateDayFormat(String dateStr, String format) {
		SimpleDateFormat dtFormat = new SimpleDateFormat(format);
		Calendar c1 = Calendar.getInstance();
		if (dateStr == null || dateStr.equals("")) {
		} else {
			c1.setTime(DateUtil.parse(dateStr, format));
		}
		c1.add(GregorianCalendar.DATE, -1);
		String dstr = dtFormat.format(c1.getTime());
		return dstr;
	}

	/**
	 * 取输入日期的ts天数据(ts负值代表上ts天，正值代表下ts天)
	 * 
	 * @param dateStr
	 *            输入日期字符串。null或空位默认当前日期
	 * @param format
	 *            字符串输入日期格式和返回格式
	 * @return
	 */
	public static String preNextDateDayFormat(String dateStr, String format,
			int ts) {
		SimpleDateFormat dtFormat = new SimpleDateFormat(format);
		Calendar c1 = Calendar.getInstance();
		if (dateStr == null || dateStr.equals("")) {
		} else {
			c1.setTime(DateUtil.parse(dateStr, format));
		}
		c1.add(GregorianCalendar.DATE, ts);
		String dstr = dtFormat.format(c1.getTime());
		return dstr;
	}

	/**
	 * 按天数返回对应的日期
	 * 
	 * @param ksrq
	 *            输入开始日期
	 * @param ts
	 *            对应的天数
	 * @param regs
	 *            返回日期的格式默认是yyyy-MM-dd
	 * @return
	 */
	public static String tsRqRetrun(String ksrq, int ts, String regs) {
		String reRq = "";
		// 获得当前日期
		Date date = DateUtil.parse(ksrq);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, ts);
		if (regs == null || regs.equals("")) {
			regs = "yyyy-MM-dd";
		}
		DateFormat df = new SimpleDateFormat(regs);
		reRq = df.format(calendar.getTime());
		return reRq;
	}

	/**
	 * 判断输入年返回所有的周次信息。i和第i周（i为1-9的时候补0）
	 * 
	 * @param yy
	 *            默认年月格式是2011这种格式
	 * @return
	 */
	public static List getZcListWithyyyy(String yy) {
		List reList = new ArrayList();
		List list = new ArrayList();
		list = getWeekWithyyyy(yy);
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				if (i + 1 < 10) {
					String s = "0" + (i + 1);
					reList.add(s);
				} else {
					reList.add("" + (i + 1));
				}
			}
		}
		return reList;
	}

	/**
	 * 判断输入年返回所有的周1;周日的day格式：2010-11-10;2011-01-17
	 * 
	 * @param yy
	 *            默认年月格式是2011这种格式,如果ny为空。则默认为当前时间
	 * @return
	 */
	public static List getWeekWithyyyy(String yy) {
		List list = new ArrayList();
		GregorianCalendar d = new GregorianCalendar();
		if (yy == null || "".equals(yy)) {
		} else {
			d.setTime(DateUtil.parse(yy, "yyyy"));
		}
		int today = d.get(Calendar.DAY_OF_YEAR);
		int year = d.get(Calendar.YEAR);
		d.set(Calendar.DAY_OF_YEAR, 1);
		int weekday = d.get(Calendar.DAY_OF_WEEK);
		// for(int i=Calendar.SUNDAY;i<weekday;i++)
		do {
			int day = d.get(Calendar.DAY_OF_YEAR);
			if (weekday == Calendar.MONDAY) {
				String monday = DateUtil.format(d.getTime(), "yyyy-MM-dd");
				String sunday = getSunday(DateUtil.parse(monday));
				list.add(monday + ";" + sunday);
			}
			d.add(Calendar.DAY_OF_YEAR, 1);
			weekday = d.get(Calendar.DAY_OF_WEEK);
		} while (d.get(Calendar.YEAR) == year);
		return list;
	}

	/**
	 * 判断输入当前的日期。在当前是第几周
	 * 
	 * @param rq
	 *            格式为2011-01-17
	 * @return
	 */
	public static int getWeekWithRq(String rq) {
		int r = 0;
		String yy = "";
		yy = rq.substring(0, 4);
		List list = DateUtil.getWeekWithyyyy(yy);
		String mday = DateUtil.getMonday(DateUtil.parse(rq));
		String sday = DateUtil.getSunday(DateUtil.parse(rq));

		String rqfw = mday + ";" + sday;
		for (int i = 0; i < list.size(); i++) {
			String str = (String) list.get(i);
			if (str.equals(rqfw)) {
				r = i + 1;
				break;
			}
		}
		return r;
	}

	/**
	 * 输入年周次返回对应的起始、结束时间（type：0起始时间，type：1结束时间）
	 * 
	 * @param nyzc
	 * @param type
	 * @return
	 */
	public static String getdqzrq(String nyzc, int type) {
		String restr = "";
		String ny = nyzc.substring(0, 4);
		String zc = nyzc.substring(4);
		if (zc.substring(0, 1).equals("0")) {
			zc = zc.substring(1);
		}
		List list = getWeekWithyyyy(ny);
		int zcint = Integer.parseInt(zc);
		String rqfw = (String) list.get(zcint - 1);
		if (type == 0) {
			restr = rqfw.split(";")[0];
		} else if (type == 1) {
			restr = rqfw.split(";")[1];
		}
		return restr;
	}

	/*
	 * 返回当前周周日
	 */
	public static String getSunday(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		// 如果当前日期不是周日则自动往后递增日期
		while (cal.get(cal.DAY_OF_WEEK) != cal.SUNDAY) {
			cal.add(Calendar.DAY_OF_WEEK, 1);
		}

		return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
	}

	/*
	 * 返回当前周周一
	 */
	public static String getMonday(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		if (c.get(c.DAY_OF_WEEK) == c.SUNDAY) {
			c.add(Calendar.DAY_OF_WEEK, -1);
		}
		c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
	}

	/**
	 * 判断输入日期段返回阶段内所有的周1;周日的day格式：2010-11-10;2011-01-17
	 * 
	 * @param
	 * @return
	 */
	public static List getWeekWithqszzrq(String qsrq, String zzrq) {
		List list = new ArrayList();
		GregorianCalendar d = new GregorianCalendar();

		d.setTime(DateUtil.parse(qsrq, "yyyy-MM-dd"));

		int today = d.get(Calendar.DAY_OF_YEAR);
		// d.set(Calendar.DAY_OF_YEAR,1);
		int weekday = d.get(Calendar.DAY_OF_WEEK);
		// for(int i=Calendar.SUNDAY;i<weekday;i++)
		do {

			if (weekday == Calendar.MONDAY) {
				String monday = DateUtil.format(d.getTime(), "yyyy-MM-dd");
				System.out.println(monday);
				String sunday = getSunday(DateUtil.parse(monday));
				int week = getWeekWithRq(monday);
				Map map = new HashMap();
				String key = "";
				if (week <= 9) {
					key = monday.substring(0, 4) + "0" + week;
				} else {
					key = monday.substring(0, 4) + "" + week;
				}
				map.put(key, monday + ";" + sunday);
				list.add(map);
			}
			d.add(Calendar.DAY_OF_YEAR, 1);
			weekday = d.get(Calendar.DAY_OF_WEEK);
		} while (differenceOfDay(DateUtil.parse(zzrq, "yyyy-MM-dd"), d
				.getTime()) >= 0);
		return list;
	}

	/**
	 * 返回输入年份的第一周
	 * 
	 * @param yy
	 *            输入年份，格式例如2011
	 * @return
	 */
	public static String getMondayWithyyyy(String yy) {
		List list = getWeekWithyyyy(yy);
		String monday = list.get(0).toString().split(";")[0];
		return monday;
	}

	/**
	 * 根据日期，得到该日期本年第一天为多少
	 * 
	 * @param rq
	 * @return 该年第一天
	 */
	public static String getFirstDayOfYear(String rq) {
		String strResult = "";
		strResult = rq.substring(0, 4) + "-01-01";
		return strResult;
	}

	/**
	 * 根据日期，得到该日期本年最后一天为多少
	 * 
	 * @param rq
	 * @return 该年第一天
	 */
	public static String getLastDayOfYear(String rq) {
		String strResult = "";
		strResult = rq.substring(0, 4) + "-12-31";
		return strResult;
	}


	/**
	 * 将格式是2011年1月1日这些格式的数据替换成fh对应符号间隔的数据
	 * 
	 * @param rq
	 * @param fh
	 * @return
	 */
	public static String getFormatRq(String rq, String fh) {
		String restr = "";
		if (rq != null && !rq.equals("")) {
			rq = rq.replace("年", fh);
			rq = rq.replace("月", fh);
			rq = rq.replace("日", fh);
			rq = rq.substring(0, rq.length() - 1);
			restr = rq;
		}
		return restr;
	}

	/**
	 * 将格式是2011-01-01这些格式的数据转换为2011年01月01日
	 * 
	 * @param rq
	 * @param fh
	 * @return
	 */
	public static String getFormatNyrRq(String rq) {
		String rerq = "";
		String[] rqs = rq.split("-");
		if (rqs.length >= 3) {
			rerq = rqs[0] + "年" + rqs[1] + "月" + rqs[2] + "日";
		}
		return rerq;
	}

	/**
	 * 判断日期（带小时）是否大于指定的小时
	 * 
	 * @param format
	 * @param h
	 *            (小时对应的点，1-24)
	 * @return
	 */
	public static boolean isGreatThanHH(Date date, int h) {
		String s2 = DateUtil.format(date) + " " + h + ":00:00";
		Date d2 = DateUtil.parse(s2, "yyyy-MM-dd HH:mm:ss");
		long l1 = date.getTime();
		long l2 = d2.getTime();
		if (l1 - l2 >= 0)
			return true;
		else
			return false;
	}
	/**
	 * 获取两个日期之间的天数
	 * 
	 */
	public static long getDaysBetweenDoubleDates(Date preDate,Date aftDate){
		long dayTimes = aftDate.getTime()-preDate.getTime();
		long days = dayTimes/(24*60*60*1000);
		return days;
	}

	public static void main(String[] args) {
//		Date edate = DateUtil.getEndDayPeriodOfTenDays(DateUtil
//				.getDate("2011-03-01"), 0);
//		Date sdate = DateUtil.getFirstDayPeriodOfTenDays(DateUtil
//				.getDate("2011-03-01"), 0);
//		java.lang.Math.abs(5);
//		System.out.println(DateUtil.format(edate));
		long days = getDaysBetweenDoubleDates(DateUtil.parse("2011-11-8"), DateUtil.parse("2011-12-9"));
		System.out.println(days);
	}
}
